define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chatgroupcontent/index?ids='+Config.ids +location.search,
                    add_url: 'chatgroupcontent/add',
                    edit_url: 'chatgroupcontent/edit',
                    del_url: 'chatgroupcontent/del',
                    multi_url: 'chatgroupcontent/multi',
                    import_url: 'chatgroupcontent/import',
                    table: 'chat_group_content',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'user.username', title: __('聊天人昵称'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user.tel', title: __('User.tel'), operate: 'LIKE'},
                        // {field: 'group_id', title: __('Group_id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'content', title: __('Content'), operate: 'LIKE', table: table, class: 'autocontent',events: Table.api.events.image,  formatter: function(value, row, index, field) {
                            var type = row.type; // 获取当前行的type字段值
                            var content = '';
                        
                            // 根据type的值显示不同内容
                            switch (type) {
                                case '1': // 文字
                                    content = value; // value本身就是文字内容，直接显示
                                    break;
                                case '2': // 图片
                                    content = '<a href="javascript:"><img class="img-sm img-center" src="' + value + '"  /> </a>'; // 假设value是图片的URL
                                    break;
                                case '3': // 视频
                                    content = '<video controls style="max-width: 100px; max-height: 100px;"><source src="' + value + '" type="video/mp4"></video>'; // 假设value是视频的URL
                                    break;
                                default:
                                    content = value; // 默认情况下显示原始值
                                    break;
                            }
                        
                            return content; // 返回格式化后的内容
                        }},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'add_time', title: __('Add_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,operate:false},
                        
                        // {field: 'group.name', title: __('Group.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
                        //*************************** 自定义export开始
                        var submitForm = function (ids, layero) {
                            var options = table.bootstrapTable('getOptions');
                            console.log(options);
                            var columns = [];
                            $.each(options.columns[0], function (i, j) {
                                if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                                    columns.push(j.field);
                                }
                            });
                            var search = options.queryParams({});
                            $("input[name=search]", layero).val(options.searchText);
                            $("input[name=ids]", layero).val(ids);
                            $("input[name=filter]", layero).val(search.filter);
                            $("input[name=op]", layero).val(search.op);
                            $("input[name=columns]", layero).val(columns.join(','));
                            $("form", layero).submit();
                        };
                        $(document).on("click", ".btn-export", function () {
                            var ids = Table.api.selectedids(table);
                            var page = table.bootstrapTable('getData');
                            var all = table.bootstrapTable('getOptions').totalRows;
                            console.log(ids, page, all);
                            Layer.confirm("<form action='" + Fast.api.fixurl("chatgroupcontent/export?ids=") +Config.ids+ "' method='post' target='_blank'><input type='hidden' name='idss' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                                title: '导出数据',
                                // btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                                btn: ["全部(" + all + "条)"],
                                success: function (layero, index) {
                                    $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                                    
                                }
                                , yes: function (index, layero) {
                                    submitForm(ids.join(","), layero);
                                    Layer.close(index);
                                    return false;
                                }
                                
                               
                            })
                        });
                        //*************************** 自定义export结束



            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
